﻿// Type: System.Windows.Controls.ContentControl
// Assembly: System.Windows, Version=5.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
// Assembly location: c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v5.0\System.Windows.dll

using MS.Internal;
using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Windows;
using System.Windows.Markup;

namespace System.Windows.Controls
{
  [ContentProperty("Content", true)]
  public class ContentControl : Control
  {
    public static readonly DependencyProperty ContentTemplateProperty = DependencyProperty.RegisterCoreProperty(10786U, typeof (DataTemplate));
    public static readonly DependencyProperty ContentProperty = DependencyProperty.RegisterCoreProperty(10785U, typeof (object));
    private object _treeContent;

    public object Content
    {
      get
      {
        return this.GetValue(ContentControl.ContentProperty);
      }
      set
      {
        this.SetValue(ContentControl.ContentProperty, value);
      }
    }

    public DataTemplate ContentTemplate
    {
      get
      {
        return (DataTemplate) this.GetValue(ContentControl.ContentTemplateProperty);
      }
      set
      {
        this.SetValue(ContentControl.ContentTemplateProperty, (DependencyObject) value);
      }
    }

    static ContentControl()
    {
    }

    public ContentControl()
      : base(373U)
    {
      this.DefaultStyleKey = (object) typeof (ContentControl);
    }

    internal ContentControl(uint nKnownTypeIndex)
      : base(nKnownTypeIndex)
    {
    }

    [SecurityCritical]
    [AllowReversePInvokeCalls]
    internal static unsafe uint OnContentChanged(IntPtr nativeTarget, ref CValue oldContentValue, ref CValue newContentValue)
    {
      try
      {
        try
        {
          object oldContent = XcpImports.ConvertType(oldContentValue, 0, true, (IManagedPeerBase) null);
          object newContent = XcpImports.ConvertType(newContentValue, 0, true, (IManagedPeerBase) null);
          object managedPeer = ManagedPeerTable.GetManagedPeer(nativeTarget);
          if (managedPeer == null)
            return 0U;
          ContentControl contentControl = managedPeer as ContentControl;
          if (contentControl == null)
            return 2147942487U;
          if (newContentValue.Type == VType.valueObject)
          {
            object obj = (object) ManagedPeerTable.GetManagedPeerWithReference(new IntPtr(newContentValue.m_pdoValue));
            contentControl._treeContent = obj;
          }
          else
            contentControl._treeContent = (object) null;
          if (oldContent == newContent || oldContent != null && oldContent.Equals(newContent))
            return 0U;
          contentControl.OnContentChanged(oldContent, newContent);
          return 0U;
        }
        finally
        {
          ManagedPeerTable.UnpegManagedPeerNoRef(newContentValue);
        }
      }
      catch (Exception ex)
      {
        return Error.GetXresultForException(ex);
      }
    }

    protected virtual void OnContentChanged(object oldContent, object newContent)
    {
    }

    internal void SetContentIsNotLogical()
    {
      XcpImports.ContentControl_SetContentIsNotLogical(this);
    }

    internal override string GetPlainText()
    {
      return FrameworkElement.GetStringFromObject(this.Content);
    }
  }
}
